package ui.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dispatchapp.andlei.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.SelectSchoolAdapter;
import base.BaseBean;
import base.activity.BaseLayoutActivity;
import bean.SchoolBean;
import netWork.NetUtils;
import utils.GsonUtils;
import widget.ClearEditText;

/**
 * 选择学校
 *
 * @author Andlei
 * @DATE 2019/1/23
 */
public class SelectSchoolActivity extends BaseLayoutActivity {
    private NetUtils netUtils;
    private ImageView img_back;
    private ClearEditText clearEditText;
    private RecyclerView recyclerView;
    private Button btn_commit;
    private SelectSchoolAdapter adapter;
    private String SchoolName = "";
    private String school_id = "";
    private List<SchoolBean> list = new ArrayList<>();
    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_school;
    }

    @Override
    protected void findViews() {
        netUtils = new NetUtils(mActivity,netRequestCallBack);
        img_back = getView(R.id.img_back);
        btn_commit = getView(R.id.btn_commit);
        clearEditText = getView(R.id.edittext_search);
        recyclerView = getView(R.id.list_rcy);
    }

    @Override
    protected void formatViews() {
        img_back.setOnClickListener(this);
        btn_commit.setOnClickListener(this);
        clearEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ((actionId == 0 || actionId == 3) && event != null) {
                    //搜索
                    Map<String,Object> map = new HashMap<>();
                    map.put("keyword",""+clearEditText.getText().toString());
                    netUtils.get("base/schools",map,BaseBean.class,null,false);
                }
                return false;
            }
        });

    }

    @Override
    protected void formatData() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new SelectSchoolAdapter(R.layout.item_select_school,list);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for(int i = 0;i<list.size(); i++){
                    if(i == position){
                        list.get(position).isSelect = true;
                        adapter.notifyDataSetChanged();
                        school_id = list.get(position).getSchool_id();
                        SchoolName = list.get(position).getName();
                    }else {
                        list.get(i).isSelect = false;
                    }
                }
            }
        });
        Map<String,Object> map = new HashMap<>();
        map.put("keyword",""+clearEditText.getText().toString());
        netUtils.get("base/schools",map,BaseBean.class,null,false);
    }

    @Override
    protected void getBundle(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.img_back:
                Intent intent = new Intent();
                intent.putExtra("school_id",school_id);
                intent.putExtra("SchoolName",SchoolName);
                setResult(11,intent);
                finish();
                break;
            case R.id.btn_commit:
                Intent intent1 = new Intent();
                intent1.putExtra("school_id",school_id);
                intent1.putExtra("SchoolName",SchoolName);
                setResult(11,intent1);
                finish();
                break;
        }
    }
    private NetUtils.NetRequestCallBack netRequestCallBack = new NetUtils.NetRequestCallBack() {
        @Override
        public void error(String action, Throwable e, Map tag) {

        }

        @Override
        public void success(String action, BaseBean t, Map tag) {
            list = GsonUtils.getInstance(mActivity).parseNoHeaderJArray(t,SchoolBean.class);
            adapter.setNewData(list);
        }
    };
}
